import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mypeiwan/pages/reservationpeiwan.dart';
import 'package:mypeiwan/utils/evaluatebean.dart';
import 'package:mypeiwan/utils/peiwaninfobean.dart';
import 'package:mypeiwan/widgets/commentitem.dart';
import 'package:mypeiwan/widgets/gender.dart';

class PeiwanInfoDetail extends StatefulWidget {
  int peiwanId;

  PeiwanInfoDetail(this.peiwanId, {super.key});

  @override
  State<StatefulWidget> createState() {
    return _PeiwanInfoDetailState();
  }
}

class _PeiwanInfoDetailState extends State<PeiwanInfoDetail> {
  late PeiWanInfoBean infoDetail;
  late int goodRating;

  @override
  void initState() {
    super.initState();
    List<EvaluateBean> list = List.generate(
      10,
      (index) {
        return new EvaluateBean(
            index,
            index + 1,
            "张三",
            new DateTime(2011, 2, 22, 3),
            "服务不错",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1521521080,4202933966&fm=253&gp=0.jpg");
      },
    );
    infoDetail = new PeiWanInfoBean(
        1,
        "施莉",
        23,
        166,
        30,
        ["逛街看电影", "颜值"],
        "本人服务态度好",
        0.3,
        0,
        44,
        list,
        "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1521521080,4202933966&fm=253&gp=0.jpg");
    print(widget.peiwanId);
  }

  double _calculateGoodRating(List<EvaluateBean> evaluateBeans) {
    var goodCount = 0;
    List.generate(evaluateBeans.length, (index) {
      EvaluateBean evaluateBean = evaluateBeans[index];
      if (evaluateBean.score >= 4) {
        goodCount += 1;
      }
    });
    return goodCount / evaluateBeans.length;
  }

  int _getGoodRating(List<EvaluateBean> evaluateBeans) {
    double x = _calculateGoodRating(evaluateBeans);
    return x as int;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        bottomNavigationBar: Container(
          padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TextButton(onPressed: () {}, child: Text("和他聊聊")),
              TextButton(
                  onPressed: () {
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) =>
                            ReservationPeiwan(widget.peiwanId),
                      ),
                    );
                  },
                  child: Text("预约达人"))
            ],
          ),
        ),
        appBar: AppBar(
          title: Text("陪玩详情"),
        ),
        body: ListView(
          children: [
            Stack(children: [
              Container(
                  height: 200,
                  width: 400,
                  child: Image.asset(
                    "images/peiwan_picture.png",
                    fit: BoxFit.cover,
                  )),
              Container(
                decoration: const BoxDecoration(
                  color: Colors.black45,
                ),
                child: Row(children: [
                  CircleAvatar(
                    backgroundColor: Colors.greenAccent,
                    radius: 2,
                  ),
                  Text(
                    "在线",
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ]),
              ),
            ]),
            Card(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(infoDetail.name),
                    Text("${infoDetail.height}CM"),
                    Text("${infoDetail.weight}KG"),
                    Gender(30, 10, 23, false),
                    SizedBox(
                      width: 120,
                    ),
                    Text("${infoDetail.hourPrice}/每小时")
                  ],
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: List.generate(
                          infoDetail.lables.length,
                          (index) {
                            return Row(
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                    color: Colors.blueAccent,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(8)),
                                  ),
                                  child: Text(infoDetail.lables[index]),
                                ),
                                SizedBox(
                                  width: 10,
                                )
                              ],
                            );
                          },
                        ),
                      ),
                      SizedBox(
                        width: 160,
                      ),
                      Icon(
                        Icons.location_on_sharp,
                        color: Colors.blue,
                        size: 20,
                      ),
                      Text("${infoDetail.distance}km")
                    ]),
                Text(
                  infoDetail.summary,
                  softWrap: true,
                  textAlign: TextAlign.left,
                )
              ],
            )),
            Card(
              child: Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Icon(
                        Icons.comment,
                        color: Colors.blueAccent,
                      ),
                      Text("评价"),
                      Text(
                        "${infoDetail.evaluateBeans.length}条评价",
                        style: TextStyle(fontSize: 12),
                      ),
                      SizedBox(
                        width: 120,
                      ),
                      Text(
                        "好评率${_calculateGoodRating(infoDetail.evaluateBeans) * 100}%",
                        style: TextStyle(color: Colors.red),
                      ),
                      IconButton(
                          onPressed: () {},
                          icon: Icon(
                            Icons.keyboard_arrow_right_sharp,
                            size: 20,
                          )),
                    ],
                  ),
                  Column(
                    children: List.generate(
                        infoDetail.evaluateBeans.length >= 5
                            ? 5
                            : infoDetail.evaluateBeans.length, (index) {
                      EvaluateBean bean = infoDetail.evaluateBeans[index];
                      return Column(children: [
                        CommentItem(
                            NetworkImage(bean.headPicUrl),
                            bean.customerName,
                            bean.score,
                            bean.dateTime,
                            bean.desc),
                        Divider(
                          height: 2.0,
                          indent: 0.0,
                          color: Colors.blueAccent,
                        ),
                      ]);
                    }),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
